package org.redmagic.service.utils;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.redmagic.domain.Property;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;

public class MongoClient {

	private final DBCollection properties;
	private final DBObject fields;
	
	public MongoClient(String databaseName, String collectionName) throws UnknownHostException {
    
		Mongo mongo = new Mongo();
		DB database = mongo.getDB(databaseName);
		this.properties = database.getCollection(collectionName);
		this.fields = new BasicDBObject(MongoProperties.ID, 0);
		this.fields.put(MongoProperties.VALUE_KEY, 1);
	}
	
	public void insert(Property property, String user) {
		DBObject insertObject = MongoUtils.getInsertObject(property, user);
		this.properties.insert(insertObject, WriteConcern.SAFE);
	}
	
	public static void main(String[] args) throws UnknownHostException {
		
		MongoClient client = new MongoClient("redmagic", "properties");
		List<String> tags = new ArrayList<String>();
		tags.add("release=1.0");
		Property property1 = new Property("database/user", tags, "USER");
		Property property2 = new Property("database/host", tags, "HOST");
		Property property3 = new Property("database/password", tags, "PASSWORD");
		client.insert(property1, "dan");
		client.insert(property2, "dan");
		client.insert(property3, "dan");
		System.out.println("Done!");
	}
	
//	{ "_id" : { "base" : "baseball" , "tags" : [ "city=stl"]} , "value" : "cardinals" , "odometer" : 0 , "creation_date" : { "$date" : "2012-10-25T02:27:41.779Z"} , "creation_user" : "localhost" , "last_modified_date" : { "$date" : "2012-10-25T02:27:41.779Z"} , "last_modified_user" : "localhost"}
}
